package com.quickask.operate.sms;

/**
 * Created by Administrator on 2015/9/24.
 */
public class SmsVerifyKit {

    private String appkey;
    private String phone;
    private String zone;
    private String code;

    /**
     *
     * @param appkey 应用KEY
     * @param phone 电话号码 xxxxxxxxx
     * @param zone 区号 86
     * @param code 验证码 xx
     */
    public SmsVerifyKit(String appkey, String phone, String zone, String code) {
        super();
        this.appkey = appkey;
        this.phone = phone;
        this.zone = zone;
        this.code = code;
    }

    public String sendMsg() throws Exception {
        String address = "https://web.sms.mob.com/sms/sendmsg";
        MobClient client = null;
        try {
            client = new MobClient(address);
            client.addParam("appkey", appkey).addParam("phone", phone)
                    .addParam("zone", zone);
            client.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            client.addRequestProperty("Accept", "application/json");
            String result = client.post();
            System.out.println(result);
            return result;
        } finally {
            client.release();
        }
    }

    /**
     * 服务端发起验证请求验证移动端(手机)发送的短信
     * @return
     * @throws Exception
     */
    public String checkCode() throws Exception {
        String address = "https://web.sms.mob.com/sms/verify";
        MobClient client = null;
        try {
            client = new MobClient(address);
            client.addParam("appkey", appkey).addParam("phone", phone)
                    .addParam("zone", zone).addParam("code", code);
            client.addRequestProperty("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
            client.addRequestProperty("Accept", "application/json");
            String result = client.post();
            System.out.println(result);
            return result;
        } finally {
            client.release();
        }
    }

    public static void main(String[] args) throws Exception {
        new SmsVerifyKit("a22acc60ca00", "13560411384", "86", "9556").sendMsg();
    }
}
